import { defineStore } from "pinia";
import { paymentInit, getBalance } from "../api/wallet.js";

export const mainWallet = defineStore("walletmain", {
  state: () => {
    return {
      balance: 0, //余额

      showBank: false, //是否显示钱包取款页面
      banckDetails: {}, //银行卡信息
      showRecharge: false, //是否显示充值页面
    };
  },
  getters: {},

  actions: {
    async getBalanceNumll(login, password) {
      try {
        const res = await getBalance();
        if (res.errno == 20005 || res.errno == 10015 || res.errno == 10204)
          return true;
        if (res.data.balance) {
          this.balance = res.data.balance;
        }
      } catch (error) {
        // 让表单组件显示错误
        return error;
      }
    }, 
    getBankInfo(type) {
      
      paymentInit().then((res) => {
        if (res.errno == 0) {
          this.banckDetails = res.data;

          switch (type) {
            case 'showRecharge':
              this.showRecharge = true;
              break;
            case 'showBank':
                this.showBank = true;
                break;
          }
        }
      })
    },
  },

  persist: {
    enabled: true,
    detached: true,
    strategies: [
      {
        key: "balance",
        storage: sessionStorage,
        paths: ["balance"]
      }
    ]
  }
});
